﻿var readyToUpdate = 0;

var currentpage = 1;
function slidePages(nextpage) {

    var slidepx = $(".img-list").width();
    if (currentpage > nextpage) {
        // slide to right
        $('ul#pagination').animate({ marginLeft: "+=" + (currentpage - nextpage) * slidepx }, { duration: 'slow' });
    }
    else {
        if (currentpage < nextpage) {
            // slide to left
            $('ul#pagination').animate({ marginLeft: "-=" + (nextpage - currentpage) * slidepx }, { duration: 'slow' });
        }
    }
}


/**
*   Tools Function
*/

function centerBoxInsideParent(box, parent) {
    var boxPositionTop = (parent.outerHeight() / 2) - (box.outerHeight() / 2);
    var boxPositionLeft = (parent.outerWidth() / 2) - (box.outerWidth() / 2);
    box.css({
        'top': boxPositionTop + 'px',
        'left': boxPositionLeft + 'px'
    });
}

function centerChildren(imgDisplay) {
    var delDiv = imgDisplay.children('.del-background');
    var delConfirm = imgDisplay.children('.del-confirm');
    var loading = imgDisplay.children('.loading');

    var detailsDiv = imgDisplay.children('.img-details');
    var detailsModifyDiv = imgDisplay.children('.img-details-modify');

    delDiv.height(detailsDiv.height() + imgDisplay.height());
    //    centerBoxInsideParent(delConfirm, delDiv);
    centerBoxInsideParent(delConfirm, imgDisplay);

    //loading.height(detailsModifyDiv.height() +imgDisplay.height());
    centerBoxInsideParent(loading, imgDisplay);
}


function positionningImageDisplay(img) {
    var imgContainer = $(img).children('.img-container');
    var imgDetails = $(img).children('.img-details');
    var imgDetailsEdition = $(img).children('.img-details-modify');
    var x = parseInt(imgContainer.css('left'), 10);
    var leftPositionInt = parseInt(imgContainer.position().left, 10) - parseInt(imgContainer.css('border-left-width'), 10)
    imgDetails.css('left', leftPositionInt + 'px');
    imgDetailsEdition.css('left', leftPositionInt + 'px');
}


$(function () {
    $(window).load(function () {
        //css calculation
        var jPaginateWidth = 10;
        $('.jPaginate').children('div').each(function () {
            jPaginateWidth += $(this).outerWidth(false);
        });
        jPaginateWidth = jPaginateWidth - parseInt($('.jPaginate').css('padding-left'));

        $('.jPaginate').css('width', jPaginateWidth + 'px');
        var ulWidth = 5;
        $('ul#pagination').children('li').each(function () {
            $(this).css('width', $('div.img-list').outerWidth(false) + 'px');
        });
        ulWidth += $('ul#pagination').children('li').length * $('div.img-list').outerWidth(false);
        $('ul#pagination').css('width', ulWidth + 'px');

        $('.img-display').each(function () {
            var img = $(this);
            centerChildren(img);
            positionningImageDisplay(img);
        });
    });

    $(document).ready(function () {

    });
    $('#pager').paginate({
        count: $('ul#pagination').children('li').length,
        start: 1,
        display: 3,
        border: false,
        text_color: '#888',
        background_color: '#EEE',
        text_hover_color: 'black',
        background_hover_color: '#CFCFCF',
        mouse: 'press',
        onChange: function (page) {
            $('._current', '#pagination').removeClass('_current');
            $('#p' + page).addClass('_current');
            slidePages(page);
            currentpage = page;
        }
    });
});

